%*******************************************************
%** This program is specifically written to replicate **
%** Xtreg in Hansford and Gomez's (2010) APSR article **
%** Created by Min-Hua Huang, Jan 25, 2012            **
%** Model: Fixing at Minimum                          **
%*******************************************************
clear; clc; format shortG;
load HansfordGomez_Data.mat;
code=HansfordGomez_Data(:,2);
y=HansfordGomez_Data(:,27);
n=size(y,1); 
x0=[HansfordGomez_Data(:,26) HansfordGomez_Data(:,3) HansfordGomez_Data(:,25) HansfordGomez_Data(:,29) HansfordGomez_Data(:,11:23)];
grandmeanx0=mean(x0);
m=size(x0,2);
obs=(1:n)';
replicate=[y x0 code obs];
replicate=sortrows(replicate,19);
dmean=dmean1(replicate,19);
y2=[dmean(:,1) replicate(:,19:20)];
mean4a=[(replicate(:,1:18)-dmean) replicate(:,19:20)];
mean4a=sortrows(mean4a,20);
y=mean4a(:,1); y2=sortrows(y2,3);
y2=y2(:,1); % district mean
wfmin=ones(n,2); wf1min=ones(n,2); 
for i=1:1:2
    wfmin(:,i)=mean4a(:,i+1)-min(mean4a(:,i+1)); 
end
cr1=HansfordGomez_Data(:,10).*wfmin(:,2);
cr2=wfmin(:,1).*wfmin(:,2);
afixx=[ones(n,1) wfmin cr1 cr2 HansfordGomez_Data(:,11:23)];
ols_afix=(afixx'*afixx)\(afixx'*y);
%yc=y+mean(y);
%save afixx.txt afixx -ascii;
%save yc.txt yc -ascii;
maxfixmin=max(y2+afixx*ols_afix);
minfixmin=min(y2+afixx*ols_afix);
%**********************************************************************
xmax=zeros(m+1,1); xmin=zeros(m+1,1);
   for i=1:1:m+1
       xmax(i)=max(afixx(:,i));
       xmin(i)=min(afixx(:,i));
   end    
xlimit=[xmax xmin];   
v=vsign(ols_afix);
fixminpredict=range(ols_afix,xlimit,v,y2,13);
fixminbv=sum(fixminpredict(:,1)<-10^-4)+sum(fixminpredict(:,2)>100+10^-4);
% **********************************************************
fprintf('\nReplication with "Fixing at Minimum" Specification\n')
fprintf('\nNumber of Boundary Violations: %3.0f\n', fixminbv);
fprintf('\nPredicted Value by Varying     Min   Max\n');
fprintf('All Variables                  %4.1f  %4.1f\n',fixminpredict(1,1),fixminpredict(1,2));
fprintf('Constant                       %4.1f  %4.1f\n',fixminpredict(2,1),fixminpredict(2,2));
fprintf('Partisan Composition           %4.1f  %4.1f\n',fixminpredict(3,1),fixminpredict(3,2));
fprintf('Turnout                        %4.1f  %4.1f\n',fixminpredict(4,1),fixminpredict(4,2));
fprintf('Turnout*GOP Incumbent          %4.1f  %4.1f\n',fixminpredict(5,1),fixminpredict(5,2));
fprintf('Turnout*Partisan Composition   %4.1f  %4.1f\n',fixminpredict(6,1),fixminpredict(6,2));
fprintf('Yr52                           %4.1f  %4.1f\n',fixminpredict(7,1),fixminpredict(7,2));
fprintf('Yr56                           %4.1f  %4.1f\n',fixminpredict(8,1),fixminpredict(8,2));
fprintf('Yr60                           %4.1f  %4.1f\n',fixminpredict(9,1),fixminpredict(9,2));
fprintf('Yr64                           %4.1f  %4.1f\n',fixminpredict(10,1),fixminpredict(10,2));
fprintf('Yr68                           %4.1f  %4.1f\n',fixminpredict(11,1),fixminpredict(11,2));
fprintf('Yr72                           %4.1f  %4.1f\n',fixminpredict(12,1),fixminpredict(12,2));
fprintf('Yr76                           %4.1f  %4.1f\n',fixminpredict(13,1),fixminpredict(13,2));
fprintf('Yr80                           %4.1f  %4.1f\n',fixminpredict(14,1),fixminpredict(14,2));
fprintf('Yr84                           %4.1f  %4.1f\n',fixminpredict(15,1),fixminpredict(15,2));
fprintf('Yr88                           %4.1f  %4.1f\n',fixminpredict(16,1),fixminpredict(16,2));
fprintf('Yr92                           %4.1f  %4.1f\n',fixminpredict(17,1),fixminpredict(17,2));
fprintf('Yr96                           %4.1f  %4.1f\n',fixminpredict(18,1),fixminpredict(18,2));
fprintf('Yr2000                         %4.1f  %4.1f\n\n',fixminpredict(19,1),fixminpredict(19,2));
